/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dmcn.bl.genetics.execute;

import dmcn.bl.genetics.fitnessfunction.AssignTerminalFitnessFunction;
import dmcn.bl.genetics.genes.Concetrator;
import javax.swing.JFrame;
import org.jgap.Chromosome;
import org.jgap.Configuration;
import org.jgap.Gene;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import org.jgap.InvalidConfigurationException;
import org.jgap.impl.CrossoverOperator;
import org.jgap.impl.DefaultConfiguration;
import org.jgap.impl.IntegerGene;
import org.jgap.impl.MutationOperator;

/**
 *
 * @author Home
 */
public class ExecuteGenetic {

    Concetrator[] concetratorGenes;
    private int numOfConcetrators;
    private int numOfTerminals;


    public void executeGenetic(int numOfConcetrators,
            int numOfTerminals,
            int populationSize,
            int numOfEvolves,
            boolean moreOptions,
            int mutation,
            double crossOver) throws InvalidConfigurationException {
        long startTime = System.currentTimeMillis();
//        this.concetratorGenes = concetratorGenes;
        this.numOfConcetrators = numOfConcetrators;
        this.numOfTerminals = numOfTerminals;
        Configuration conf = new DefaultConfiguration();
        if (moreOptions) {
            conf.addGeneticOperator(new CrossoverOperator(conf, crossOver));
            conf.addGeneticOperator(new MutationOperator(conf, mutation));
        }
        conf.setPreservFittestIndividual(true);
        AssignTerminalFitnessFunction ff = new AssignTerminalFitnessFunction(numOfConcetrators, numOfTerminals);
        conf.setFitnessFunction(ff);
        //edw bazeis 0,1
        Gene[] sampleGenes = new Gene[numOfConcetrators * numOfTerminals];
        for (int i = 0; i < numOfConcetrators * numOfTerminals; i++) {
            sampleGenes[i] = new IntegerGene(conf, 0, 1);
        }
        IChromosome sampleChromosome = new Chromosome(conf, sampleGenes);
        conf.setSampleChromosome(sampleChromosome);
        conf.setPopulationSize(populationSize);
        Genotype population;
        population = Genotype.randomInitialGenotype(conf);
        for (int i = 0; i < numOfEvolves; i++) {
            population.evolve();

        }
        IChromosome bestSolutionSoFar = population.getFittestChromosome();
       
            
            System.out.println("The best solution has a fitness value of "
                    + bestSolutionSoFar.getFitnessValue());


        

//        for (int i = 0; i < this.numOfConcetrators; i++) {
//            this.concetratorGenes[i].setTerminal((Integer) bestSolutionSoFar.getGene(i).getAllele());
//            f.repaint();
//        }
        long endTime = System.currentTimeMillis();
        System.out.println("execute at : " + (endTime - startTime) / 1000 + " seconds");
        Configuration.reset();

    }
//        public Concetrator[] getConcetratorGenes() {
//        return concetratorGenes;
//    }
}
